package com.exigenservices.practice.roadcompanion.server.dao;

import com.exigenservices.practice.roadcompanion.server.entity.User;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Repository;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Konstantin
 * Date: 11.07.13
 * Time: 15:05
 * To change this template use File | Settings | File Templates.
 */

@Repository
public class UsersDAOimpl implements UsersDAO {
    private static Logger LOGGER = Logger.getLogger(UsersDAOimpl.class.getName());

    @PersistenceContext(unitName = "users")
    private EntityManager entityManager;

    public UsersDAOimpl(EntityManager entityManager){
        this.entityManager= entityManager;
    }
    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public User createUsers(User users) throws Exception{
        try{
            entityManager.persist(users);

            return  entityManager.find(User.class, users.getID());
        } catch (HibernateException e){
            LOGGER.error("UsersDAOimpl::Could not create User:: ",e);
            throw new Exception("Could not create User. " + e.getMessage());
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void updateUsers(User users) throws Exception{
        try {
            User u = entityManager.find(User.class, users);

            entityManager.getTransaction().begin();
            u = users;
            entityManager.getTransaction().commit();

        } catch (HibernateException e) {
            LOGGER.error("UsersDAOimpl::Could not update User:: ",e);
            //throw new Exception("Could not update User. " + e.getMessage());
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void deleteUsers(User users) throws Exception{
        try {
            entityManager.remove(users);
        } catch (HibernateException e) {
            LOGGER.error("UsersDAOimpl::Could not delete User:: ",e);
            //throw new Exception("Could not delete User. " + e.getMessage());
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public User getUser(Long id) throws Exception{
        try {
            User user = entityManager.find(User.class, id);

            return user;
        } catch (HibernateException e) {
            LOGGER.error("UsersDAOimpl::Could not get User by ID:: ",e);
            throw new Exception("Could not get user  by id. " + e.getMessage());
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public User getUser(String login) throws Exception{
        try {
            User user = (User) entityManager.createQuery("select User from USERS where login = ?" + login).getSingleResult();

            return user;
        } catch (HibernateException e) {
            LOGGER.error("UsersDAOimpl::Could not get User by login:: ",e);
            throw new Exception("Could not get user by login. " + e.getMessage());
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<User> getAllUsers() throws Exception{
        try {
            List<User> users = entityManager.createQuery("select User from USERS").getResultList();

            return users;
        } catch (HibernateException e) {
            LOGGER.error("UsersDAOimpl::Could not get all Users:: ",e);
            throw new Exception("Could not get all users. " + e.getMessage());
        }
    }

}

